﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Youtube_HD.Model;
using Youtube_HD.Services;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Youtube_HD.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class SearchPage : Page
    {
        public SearchPage()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private ISearchViewModel searchViewModel
        {
            get { return (ISearchViewModel)DataContext; }
        }
        private void nextPageSearch(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                    searchViewModel.MoviesSearch = null;
                    searchViewModel.Playlists = null;
                    searchViewModel.Channels = null;
                    HubData.Focus(FocusState.Programmatic);
                    if (!string.IsNullOrEmpty(searchData.Text.Trim()))
                    {
                        Dictionary<string, string> param = new Dictionary<string, string>();
                        param.Add("v","2");
                        param.Add("max-results", "25");
                        searchViewModel.YoutubeVideosSearch(searchData.Text.Trim(), param);
                        searchViewModel.YoutubePlaylistSearch(searchData.Text.Trim(), param);
                        searchViewModel.YoutubeChannelsSearch(searchData.Text.Trim(), param);
                    }
                    else
                    {
                        new MessageDialog("Please input to search").ShowAsync();
                    }

                }
            }
        }

        private void Videos_ItemClick(object sender, ItemClickEventArgs e)
        {
            var data = (YoutubeVideo)e.ClickedItem;
            if (!string.IsNullOrEmpty(data.Id))
            {
                Frame fm = Window.Current.Content as Frame;
                fm.Navigate(typeof(DetailPage), data);
            }
            else
            {
                new MessageDialog("Play List not found!").ShowAsync();
            }
        }

        private void Channels_ItemClick(object sender, ItemClickEventArgs e)
        {
            
            
        }

        private void Playlist_ItemClick(object sender, ItemClickEventArgs e)
        {
           
        }
    }
}
